package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.IProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品规格 前端控制器
 * </p>
 *
 * @author lee
 * @since 2024-07-15
 */
@Tag(name = "商品规格管理")
@RestController
@RequestMapping("/productSpec")
@RequiresLogin
public class ProductSpecController extends BaseController {
    @Autowired
    private IProductSpecService productSpecService;

    @Operation(summary = "根据分类id获取商品规格列表")
    @GetMapping("/productSpecList/{id}")
    public AjaxResult getSpecList(@PathVariable Long id){
        List<ProductSpec> productSpecList = productSpecService.getProductSpecList(id);
        return success(productSpecList);
    }


    @Operation(summary = "分页查询商品规格")
    @GetMapping("/list")
    @RequiresPermissions("product:productSpec:list")
    public TableDataInfo list(ProductSpec productSpec){
        startPage();
        List<ProductSpec> list = productSpecService.selectProductSpecList(productSpec);
        return getDataTable(list);
    }

    @Operation(summary = "获取商品规格详细信息")
    @GetMapping("{id}")
    @RequiresPermissions("product:productSpec:query")
    public AjaxResult getProductSpecDetail(@PathVariable("id") Long id){
        ProductSpec productSpec = productSpecService.getProductSpecDetail(id);
        return success(productSpec);
    }

    @Operation(summary = "新增商品规格")
    @PostMapping
    @RequiresPermissions("product:productSpec:add")
    public AjaxResult addProductSpec(@Validated @RequestBody ProductSpec productSpec){
        return toAjax(productSpecService.save(productSpec));
    }

    @Operation(summary = "修改商品规格")
    @PutMapping
    @RequiresPermissions("product:productSpec:edit")
    public AjaxResult updateProductSpec(@Validated @RequestBody ProductSpec productSpec){
        return toAjax(productSpecService.updateById(productSpec));
    }
    @Operation(summary = "删除商品规格")
    @DeleteMapping("{id}")
    @RequiresPermissions("product:productSpec:delete")
    public AjaxResult deleteProductSpec(@PathVariable("id") List<Long> ids){
        return toAjax(productSpecService.removeBatchByIds(ids));
    }
}
